﻿using project.B12.application.Ioc;
using project.B12.IService;
using project.B12.Model.Enums;
using project.B12.Model.Res;
using software.Common.MVVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WPF.Controls;

namespace project.B12.application.ViewModels.Setting
{
    public class DisinfectionProtectionViewModel:ObservableObject
    {
        private int antisepsisCountdown=2;
        public int AntisepsisCountdown
        {
            get { return antisepsisCountdown; }
            set { antisepsisCountdown = value; this.RaisePropertyChanged(); }
        }

        private string countDown;
        public string CountDown
        {
            get { return countDown; }
            set
            {
                countDown = value;
                this.RaisePropertyChanged();
            }
        }

        private bool startEnabled=false;
        public bool StartEnabled
        {
            get { return startEnabled; }
            set
            {
                startEnabled = value;
                this.RaisePropertyChanged();
            }
        }

        public RelayCommand StartDisinfection => new RelayCommand(StartImplement);

        public RelayCommand Cancel => new RelayCommand(CancelImplement);

        private IServiceDevice _serviceDevice;

        public DisinfectionProtectionViewModel(IServiceDevice serviceDevice)
        {
            _serviceDevice=serviceDevice;
            _ = WaitStartDisinfection();
        }

        private async Task WaitStartDisinfection()
        {
            await Task.Run(async () =>
            {
                while (true)
                {
                    if(_serviceDevice.IsInitEnd)
                    {
                        var incubations = _serviceDevice.Items64.Any(o => o.State == HoleState.Incubation);
                        if (!incubations && _serviceDevice.deviceState == DeviceState.空闲&& !_serviceDevice.Disinfection)
                        {
                            StartEnabled = true;
                        }
                        else
                            StartEnabled = false;
                    }
                    
                    await Task.Delay(1000);
                }
            });
        }

        private void StartImplement()
        {
            var incubations = _serviceDevice.Items64.Any(o => o.State == HoleState.Incubation);
            if (incubations)
            {
                WPF.Controls.Message.Push("不可消毒！", MessageBoxImage.Warning);
                return;
            }
            if (_serviceDevice.deviceState != DeviceState.空闲)
            {
                WPF.Controls.Message.Push("软件正在运行，不可消毒！", MessageBoxImage.Warning);
                return;
            }

            ViewModelLocator.MonitorViewModel.IsEnabled = false;
            StartEnabled = false;
            _serviceDevice.StartDisinfection(AntisepsisCountdown);
            _ = WaitDisinfection();
        }

        private void CancelImplement()
        {
            if(_serviceDevice.Disinfection)
            {
                bool result=_serviceDevice.StopDisinfection();
                if(result)
                    StartEnabled = true;
                return;
            }
            WPF.Controls.Message.Push("当前未在消毒", MessageBoxImage.Warning);
        }

        private async Task WaitDisinfection()
        {
            DateTime dateTime = DateTime.Now;
            await Task.Run(async ()=>
            {
                while(true)
                {
                    if ((dateTime+new TimeSpan(AntisepsisCountdown,0,0))>DateTime.Now)
                    {
                        CountDown = (dateTime + new TimeSpan(AntisepsisCountdown, 0, 0) - DateTime.Now).ToString(@"hh\:mm\:ss");
                    }
                    if (!_serviceDevice.Disinfection)
                    {
                        MessageBox.Show(Resources.DisinfectionDone);
                        ViewModelLocator.MonitorViewModel.IsEnabled = true;
                        CountDown = string.Empty;
                        return;
                    }
                    await Task.Delay(1000);
                }
            });
        }
    }
}
